﻿using System;
using System.Net;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Text;

namespace MilitaryMeteo
{

    public class TableValue
    {
        public int Distance { get; set; }
        public int dXw { get; set; }
        public int dXh { get; set; }
        public int dXt { get; set; }
        public int dXtz { get; set; }
        public int dXv { get; set; }
        public double Z { get; set; }
        public int dZw { get; set; }
        public int MeteoNum { get; set; }
    }

    public static class TableDataParser
    {

        private static Uri GetResourceUri(string resource)
        {
            //AssemblyName assemblyName = new AssemblyName(System.Reflection.Assembly.GetExecutingAssembly().GetName().FullName);
            string resourcePath = "/" + "MilitaryMeteo" + ";component/" + resource;
            Uri resourceUri = new Uri(resourcePath, UriKind.Relative);
            return resourceUri;
        }

        private static String GetTableText(WeaponType w_type)
        {
            string file = "";
            switch (w_type)
            {
                case WeaponType.Первый:
                    file = "/Tables/first.csv";
                    break;
                case WeaponType.Второй:
                    file = "/Tables/second.csv";
                    break;
                case WeaponType.Третий:
                    file = "/Tables/third.csv";
                    break;
                case WeaponType.Уменьшеный:
                    file = "/Tables/reduced.csv";
                    break;
                case WeaponType.Полный:
                    file = "/Tables/full.csv";
                    break;
            }
            var uri = GetResourceUri(file);
            var res = Application.GetResourceStream(uri);
            var data = new byte[res.Stream.Length];
            res.Stream.Read(data, 0, (int)res.Stream.Length);
            var text = Encoding.UTF8.GetString(data, 0, data.Length);
            return text;
        }

        public static TableValue GetTableValue(WeaponType w_type, int distance)
        {
            var table = GetTableText(w_type);
            var allLines = table.Split('\n');

            var query = from line in allLines
                        let data = line.Split(';')
                        select new TableValue()
                        {
                            Distance = int.Parse(data[0]),
                            Z = int.Parse(data[9]),
                            dZw = int.Parse(data[10]),
                            dXw = int.Parse(data[11]),
                            dXh = int.Parse(data[12]),
                            dXt = int.Parse(data[13]),
                            dXtz = int.Parse(data[14]),
                            dXv = int.Parse(data[15]),
                            MeteoNum = int.Parse(data[22])
                        };

            return query.FirstOrDefault(v => v.Distance == distance);
        }

    }
}
